package com.baizan.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.baizan.daka.base.BasePresenter
import com.baizan.daka.base.IView
import com.classic.common.MultipleStatusView


/**
 * @author xuhao
 * created: 2017/10/25
 * desc:BaseActivity基类
 */
abstract class BaseFragment<P : BasePresenter> : Fragment(),
     IView {
    /**
     * Fragment依附的Activity的mContext
     */
    var mContext: BaseActivity<*>? = null

    /**
     * 多种状态的 View 的切换
     */
    protected var mLayoutStatusView: MultipleStatusView? = null
    var presenter: P? = null
    var mRootView: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflater.inflate(layoutId(), null)
        mRootView = view
        return view
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mContext = getBaseActivity()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        presenter = initPresenter()
        initData()
        initListener()
        start()
    }

    override fun showLoadingDialog() {
        mContext?.let {
            it.showLoadingDialog()
        }
    }

    override fun hideLoadingDialog() {
        mContext?.let {
            it.hideLoadingDialog()
        }
    }

     fun showLoadingDialog(label: String) {
        mContext?.let {
            it.showLoadingDialog(label)
        }
    }

    open fun getBaseActivity(): BaseActivity<*>? {
        return this.getActivity() as BaseActivity<*>?
    }

    abstract fun initPresenter(): P?

    private fun initListener() {
        mLayoutStatusView?.setOnClickListener(mRetryClickListener)
    }

    open val mRetryClickListener: View.OnClickListener = View.OnClickListener {
        start()
    }


    /**
     *  加载布局
     */
    abstract fun layoutId(): Int

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 初始化 View
     */
    abstract fun initView()

    /**
     * 开始请求
     */
    abstract fun start()


    /**
     * 打卡软键盘
     */
    fun openKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN)
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
    }

    /**
     * 关闭软键盘
     */
    fun closeKeyBord(mEditText: EditText, mContext: Context) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mEditText.windowToken, 0)
    }


    override fun onDestroy() {
        super.onDestroy()
        presenter?.detachView()
//        MyApplication.getRefWatcher(this)?.watch(this)
    }

}


